package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import model.House;

public class DrawPanel extends JPanel {
	Image gameRules;
	Image cancelButton;
	Image gameOverDialog;
	Image bHouseImage;
	Image oJarImage;
	Image gJarImage;
	Image uHouseImage;
	Image playerNamePanel1;
	Image playerNamePanel2;
	Image playerNamePanelG;
	ImageIcon gHouseIcon;
	ImageIcon oHouseIcon;
	ImageIcon oJarIcon;
	ImageIcon gJarIcon;

	enum Rooms {
		HOME, MENU, GAMERULES, PLAYERS, START
	}

	private Rooms room = Rooms.HOME;

	public DrawPanel() {
		ImageIcon ii = new ImageIcon("gamerules.PNG");
		ImageIcon p1Panel = new ImageIcon("oplayertab.PNG");
		ImageIcon p2Panel = new ImageIcon("grplayertab.PNG");
		ImageIcon GPanel = new ImageIcon("gplayertab.PNG");
		ImageIcon GOD = new ImageIcon("gobackground.PNG");
		gHouseIcon = new ImageIcon("g0house.PNG");
		oHouseIcon = new ImageIcon("o0house.PNG");
		oJarIcon = new ImageIcon("ojar.PNG");
		gJarIcon = new ImageIcon("gjar.PNG");
		ImageIcon cancelIcon = new ImageIcon("cancelbutton.PNG");
		cancelButton = cancelIcon.getImage();
		gameRules = ii.getImage();
		uHouseImage = gHouseIcon.getImage();
		bHouseImage = oHouseIcon.getImage();
		oJarImage = oJarIcon.getImage();
		gJarImage = gJarIcon.getImage();
		playerNamePanel1 = p1Panel.getImage();
		playerNamePanel2 = p2Panel.getImage();
		playerNamePanelG = GPanel.getImage();
		gameOverDialog = GOD.getImage();
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.white);
		g2d.fillRect(0, 0, getWidth(), getHeight());
		if (room == Rooms.HOME) {
			g2d.setColor(Color.black);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
			g2d.drawString("Please click on Mancala to continue", 230, 30);
			g2d.drawString("Mr T team  Raido  Risto  Rauno  Kaarel", 230, 250);
			g2d.drawString("University of Tartu 2011", 270, 300);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 100));
			g2d.drawString("Mancala", 150, 190);
		}
		if (room == Rooms.MENU) {
			// g2d.drawImage(bardejov, 10, 10, null);
			g2d.setColor(Color.black);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
			g2d.drawString("2 player thinking game", 270, 230);
			g2d.drawString("University of Tartu 2011", 470, 350);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 100));
			g2d.drawString("Mancala", 150, 190);
		}
		if (room == Rooms.PLAYERS) {
			// g2d.drawImage(bardejov, 10, 10, null);
			g2d.setColor(Color.black);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
			g2d.drawString("University of Tartu 2011", 470, 350);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 30));
			g2d.drawString("Mancala", 280, 40);
			g2d.drawString("Player's names", 230, 140);

		}
		if (room == Rooms.START) {
			// g2d.drawImage(bardejov,
			g2d.setColor(Color.black);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
			g2d.drawString("University of Tartu 2011", 470, 350);
			g2d.setFont(new Font("Purisa", Font.PLAIN, 30));
			if (MancalaApp.getGameBoard().getPlayerTurn() == 0) {
				uHouseImage = gHouseIcon.getImage();
				bHouseImage = oHouseIcon.getImage();
				oJarImage = oJarIcon.getImage();
				gJarImage = gJarIcon.getImage();
				g2d.drawImage(playerNamePanelG, 380, 10, null);
				g2d.drawImage(playerNamePanel1, 20, 280, null);
				g2d.drawString(MancalaApp.getPlayer1().getName(), 30, 330);
				g2d.drawString(MancalaApp.getPlayer2().getName(), 390, 55);
			} else {
				bHouseImage = gHouseIcon.getImage();
				uHouseImage = oHouseIcon.getImage();
				oJarImage = gJarIcon.getImage();
				gJarImage = oJarIcon.getImage();
				g2d.drawImage(playerNamePanel2, 20, 280, null);
				g2d.drawImage(playerNamePanelG, 380, 10, null);
				g2d.drawString(MancalaApp.getPlayer1().getName(), 390, 55);
				g2d.drawString(MancalaApp.getPlayer2().getName(), 30, 330);
			}
			int houseIndex = 0;
			for (House house : MancalaApp.getHouses()) {
				if (houseIndex < 6) {
					g2d.drawImage(bHouseImage, 180 + 50 * houseIndex, 190, null);
					g2d.drawString("" + house.getQuantityOfSeeds(),
							193 + 50 * houseIndex, 228);
				} else {
					g2d.drawImage(uHouseImage, 430 + (-50 * (houseIndex - 6)),
							120, null);
					g2d.drawString("" + house.getQuantityOfSeeds(),
							443 + (-50 * (houseIndex - 6)), 158);
				}
				houseIndex++;
			}
			g2d.drawImage(gJarImage, 60, 130, null);
			g2d.drawImage(oJarImage, 500, 130, null);
			g2d.drawString(""
					+ MancalaApp.getJars().get(0).getQuantityOfSeeds(), 540,
					190);
			g2d.drawString(""
					+ MancalaApp.getJars().get(1).getQuantityOfSeeds(), 100,
					190);
			g2d.drawImage(cancelButton, 20, 30, null);
			if (MancalaApp.isGameOver()) {
				g2d.setFont(new Font("Purisa", Font.PLAIN, 30));
				g2d.drawImage(gameOverDialog, 175, 95, null);
				int p1Seeds = MancalaApp.getPlayer1().getJar().getQuantityOfSeeds();
				int p2Seeds = MancalaApp.getPlayer2().getJar().getQuantityOfSeeds();
				if (p1Seeds > p2Seeds) {
					g2d.drawString(MancalaApp.getPlayer1().getName() + " wins!", 180,
							130);
					g2d.setFont(new Font("Purisa", Font.PLAIN, 20));
					g2d.drawString("With a score of " + p1Seeds, 220, 200);
				} else if (p1Seeds < p2Seeds) {
					g2d.drawString(MancalaApp.getPlayer2().getName() + " wins!", 180,
							130);
					g2d.setFont(new Font("Purisa", Font.PLAIN, 20));
					g2d.drawString("With a score of " + p2Seeds, 220, 200);
				} else {
					g2d.drawString("It's a draw!", 180, 130);
					g2d.setFont(new Font("Purisa", Font.PLAIN, 20));
					g2d.drawString("With a score of " + p1Seeds, 220, 200);
				}
			}
		}
		if (room == Rooms.GAMERULES) {
			g2d.setFont(new Font("Purisa", Font.PLAIN, 13));
			g2d.setColor(Color.black);
			g2d.drawImage(gameRules, 50, 30, null);
			g2d.drawString("University of Tartu 2011", 470, 350);
		}
	}

	public Rooms getRoom() {
		return room;
	}

	public void setRoom(Rooms room) {
		this.room = room;
	}
}
